/************************************************************
 * $Id : CmdState.cpp   2011/08/01
 *
 *       swhors@naver.com
 ************************************************************/

#include "config.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

using namespace std;

#include "constant.h"
#include "ast_peg.h"
#include "ast_db.h"
#include "CmdBase.h"
#include "CmdState.h"
#include "ast_util.h"
#include "ast_event.h"

extern int   gIsConnected2DB;

CCmdState::CCmdState()
{
   init( );
}

CCmdState::~CCmdState()
{
   init( );
}

void CCmdState::release()
{
   super::release();
}

void CCmdState::init()
{
   super::init();
   
   memset( mState, 0, 32 );
}

void * CCmdState::parse( char * aCommandStr )
{
   char * sStartStr = NULL;
   char * sMsgStart = NULL;
   
   sMsgStart = aCommandStr;
   
   super::parse( sMsgStart );
   
   sMsgStart = aCommandStr;
   
   if ( state_f::Matches( sMsgStart, sStartStr) )
   {
      memset( mState, 0, 32 ); 
      memcpy( mState, sStartStr + 7, ( sMsgStart - sStartStr - 9) );
   }
   else
   {
      /* do nothing */
   }
   
   return this;
}
CCmdState &CCmdState::operator = ( const CCmdState & aOther )
{
   sprintf( mPrivilege, "%s", aOther.mPrivilege );
   sprintf( mChannel,   "%s", aOther.mChannel );
   sprintf( mState,     "%s", aOther.mState );
   
   return *this;
}

int CCmdState::operator < ( const CCmdState & aOther )
{
   int sRetVal = 0;
   
   sRetVal = strcmp( mPrivilege, aOther.mPrivilege );
   if ( sRetVal < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetVal = strcmp( mChannel, aOther.mChannel );
   if ( sRetVal < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   sRetVal = strcmp( mState, aOther.mState );
   if ( sRetVal < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
    
   return 1;
   
notless:
   
   return 0;
}

int CCmdState::operator > ( const CCmdState & aOther )
{
   int sRetVal = 0;
   
   sRetVal = strcmp( mPrivilege, aOther.mPrivilege );
   if ( sRetVal > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetVal = strcmp( mChannel, aOther.mChannel );
   if ( sRetVal > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   sRetVal = strcmp( mState, aOther.mState );
   if ( sRetVal > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   return 1;
   
notmore:
   
   return 0;
}

int CCmdState::operator == ( const CCmdState & aOther )
{
   int sRetVal = 0;
   
   sRetVal = strcmp( mPrivilege, aOther.mPrivilege );
   if ( sRetVal == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }
   
   sRetVal = strcmp( mChannel, aOther.mChannel );
   if ( sRetVal == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }
   
   sRetVal = strcmp( mState, aOther.mState );
   if ( sRetVal == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }
   
   return 1;
   
notequal:
   
   return 0;
}

int CCmdState::event()
{
   if ( gIsConnected2DB )
   {
      online_sql_set_confstate( mState,
                                mChannel );
      goto event_success;
   }
   else
   {
      /* do nothing */
   }
   goto event_fail;
   
event_success:
   return 1;
   
event_fail:
   return 0;
}
